<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\CircleAction;
use App\Admin\Repositories\WxCircle;
use App\Admin\Repositories\WxPlate;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Admin\Repositories\WxUser;

class WxCircleController extends AdminController
{

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WxCircle(['wxPlate','wxUser']), function (Grid $grid) {
            $grid->column('circle_name');
            $grid->column('sort')->editable();
            $grid->column('circle_introduce')->sortable()->limit(9);
            $grid->column('head_portrait')->image('','80');
            $grid->column('background_maps')->image('','80');
            $grid->column('wxPlate.plate_name')->label('default');
            $grid->column('wxUser.user_name');
            $grid->column('is_top_recommend')->switch();
            $grid->column('is_hot')->switch();
            $grid->column('circle_state')->using([0=>'审核中',1=>'审核通过',2=>'驳回'])->label([
                0=>'red',
                1=>'green',
                2=>'default'
            ]);
            $grid->column('created_at');
            $grid->model()->orderBy('id', 'desc');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('circle_name');
                $filter->equal('circle_state')->select([0=>'审核中',1=>'审核通过',2=>'驳回']);

            });
            $grid->disableCreateButton();
            // 禁用详情按钮
            $grid->disableViewButton();
            $grid->disableBatchDelete();
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->append((new CircleAction())->setKey($this->id));
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxCircle(), function (Show $show) {
            $show->field('id');
            $show->field('circle_name');
            $show->field('circle_introduce');
            $show->field('head_portrait');
            $show->field('background_maps');
            $show->field('plate_id');
            $show->field('user_id');
            $show->field('is_top_recommend');
            $show->field('is_hot');
            $show->field('circle_state');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WxCircle(), function (Form $form) {
            $form->display('id');
            $form->text('circle_name');
            $form->number('sort');
            $form->text('circle_introduce');
            $form->image('head_portrait');
            $form->image('background_maps');
            $form->select('plate_id')->options((new WxPlate())->optionsLists());;
            $form->select('user_id')->options((new WxUser())->optionsLists());;
            $form->switch('is_top_recommend');
            $form->switch('is_hot');
            $form->radio('circle_state')->options([0=>'审核中',1=>'审核通过',2=>'驳回']);

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
